package org.bees.cms.web.util.velocity.editer;

import java.io.Serializable;
import java.util.Arrays;

import org.bees.cms.web.util.Beans;

import net.sf.json.JSONObject;

public class OrderEditer extends AbstractClauseEditer {
	private String container;
	private JSONObject by;
	private JSONObject order;

	@Override
	public void initialize(JSONObject json) throws Exception {
		super.initialize(json);
		container = json == null ? container : json.containsKey("container") ? json.getString("container") : container;
		by = json == null ? by : json.containsKey("by") ? json.getJSONObject("by") : by;
		order = json == null ? order : json.containsKey("order") ? json.getJSONObject("order") : order;
	}

	@Override
	public String edit(Serializable t) throws Exception {
		StringBuilder builder = new StringBuilder();

		container = container == null || container.trim().equals("") ? name : container;
		builder.append("<div id='" + container + "'></div>");

		builder.append("<button");
		builder.append(" type='button'");
		builder.append(" onclick=\"javascript:addOrderPair(this);\"");
		builder.append(" table='" + table + "'");
		builder.append(" container='" + container + "'");
		builder.append(" by='" + by + "'");
		builder.append(" order='" + order + "'");
		builder.append(" bys='" + Arrays.toString((Object[]) Beans.get(t, by.getString("property"))).replace(", ", ",") + "'");
		builder.append(" orders='" + Arrays.toString((Object[]) Beans.get(t, order.getString("property"))).replace(" ", "") + "'");
		builder.append(" pair='true'");
		builder.append(">");
		builder.append("添加");
		builder.append("</button>");
		
		return builder.toString();
	}

	@Override
	public void destroy() throws Exception {
		super.destroy();
		container = null;
		by = null;
		order = null;
	}

	public String getContainer() {
		return container;
	}

	public void setContainer(String container) {
		this.container = container;
	}

	public JSONObject getBy() {
		return by;
	}

	public void setBy(JSONObject by) {
		this.by = by;
	}

	public JSONObject getOrder() {
		return order;
	}

	public void setOrder(JSONObject order) {
		this.order = order;
	}

}
